<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>商品管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/admin.css">
</head>
<body>
<div class="container">
    <h1>商品管理</h1>

    <c:if test="${not empty requestScope.success}">
        <div class="alert success">${requestScope.success}</div>
    </c:if>
    <c:if test="${not empty requestScope.error}">
        <div class="error">${requestScope.error}</div>
    </c:if>

    <!-- 操作按钮 -->
    <div class="actions">
        <a href="${pageContext.request.contextPath}/admin/products/add" class="btn">添加商品</a>
        <!-- 搜索表单 -->
        <div class="search-form">
            <form action="${pageContext.request.contextPath}/admin/products/search" method="get">
                <input type="text" name="keyword" placeholder="搜索商品..." value="${searchKeyword}">
                <button type="submit">搜索</button>
            </form>
        </div>

    </div>

    <!-- 商品表格 -->
    <table>
        <!-- 表头 -->
        <thead>
        <tr>
            <th>ID</th>
            <th>商品名称</th>
            <th>价格</th>
            <th>库存</th>
            <th>描述</th>
            <th>图片</th>
            <th>操作</th>
        </tr>
        </thead>

        <!-- 表格内容 -->
        <tbody>
        <c:forEach items="${requestScope.products}" var="product">
            <tr>
                <td>${product.productId}</td>
                <td>${product.name}</td>
                <td>¥${product.price}</td>
                <td>${product.stock}</td>
                <td>${product.description}</td>
                <td>
                    <c:if test="${not empty product.image}">
                        <img src="${pageContext.request.contextPath}/uploads/${product.image}"
                             alt="${product.name}" width="50">
                    </c:if>
                </td>
                <td>
                    <a href="${pageContext.request.contextPath}/admin/products/edit?id=${product.productId}" class="btn edit">编辑</a>
                    <a href="${pageContext.request.contextPath}/admin/products/delete?id=${product.productId}"
                       class="btn delete"
                       onclick="return confirm('确定要删除该商品吗？')">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

    <!-- 分页导航 - 添加在这里 -->
    <div class="pagination">
        <c:if test="${requestScope.currentPage > 1}">
            <a href="${pageContext.request.contextPath}/admin/products?page=${requestScope.currentPage - 1}">上一页</a>
        </c:if>

        <c:forEach begin="1" end="${requestScope.totalPages}" var="i">
            <c:choose>
                <c:when test="${i == requestScope.currentPage}">
                    <span class="current">${i}</span>
                </c:when>
                <c:otherwise>
                    <a href="${pageContext.request.contextPath}/admin/products?page=${i}">${i}</a>
                </c:otherwise>
            </c:choose>
        </c:forEach>

        <c:if test="${requestScope.currentPage < requestScope.totalPages}">
            <a href="${pageContext.request.contextPath}/admin/products?page=${requestScope.currentPage + 1}">下一页</a>
        </c:if>
    </div>

</div>
</body>
</html>